package rs.udd.service.mappings;

import java.util.Iterator;
import java.util.Set;

import rs.udd.model.Advert;
import rs.udd.model.Category;
import rs.udd.service.dto.advert.AdvertViewDto;

public class AdvertMapping {

	// this private constructor prevents this class from instantiation
	private AdvertMapping() {
	}

	public static AdvertViewDto view(Advert advert) {

		AdvertViewDto dto = new AdvertViewDto();

		dto.setAbstractText(advert.getAbstractText());
		dto.setAddress(advert.getAdvertiser().getAddress());
		dto.setAdvertiser(advert.getAdvertiser());
		dto.setCategories(advert.getCategories());
		dto.setCreationTime(advert.getCreationTime());
		dto.setFilePath(advert.getFilePath());
		dto.setId(advert.getId());
		dto.setKeyWords(advert.getKeyWords());
		dto.setModificationTime(advert.getModificationTime());
		dto.setPhoneNumber(advert.getAdvertiser().getPhoneNumber());
		dto.setTitle(advert.getTitle());
		dto.setCategoryNames(mapCategories(advert.getCategories()));

		return dto;
	}
	
	private static String mapCategories(Set<Category> categories) {
		String retVal = "";
		Iterator<Category> i = categories.iterator();
		while (i.hasNext()) {
			Category cat = i.next();
			retVal += cat.getName();
			
			if (i.hasNext()) {
				retVal += ", ";
			}
		}
		return retVal;
	}

}
